﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Extensions;
using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations;
using SelfService.Model.Models;
using SelfService.Model;


namespace SelfService.ViewModel.SysDeviceVMs
{
    public partial class SysDeviceListVM : BasePagedListVM<SysDeviceView, SysDeviceSearcher>
    {

        protected override IEnumerable<IGridColumn<SysDeviceView>> InitGridHeader()
        {
            return new List<GridColumn<SysDeviceView>>{
                this.MakeGridHeader(x => x.Name),
                this.MakeGridHeader(x => x.Model),
                this.MakeGridHeader(x => x.Description),
                this.MakeGridHeader(x => x.Mode),
                this.MakeGridHeader(x => x.GroupName),
                this.MakeGridHeader(x => x.Port),
                this.MakeGridHeader(x => x.DllName),
                this.MakeGridHeader(x => x.DllZipId),
                this.MakeGridHeader(x => x.DllClass),
                this.MakeGridHeader(x => x.DllWorkSpace),
                this.MakeGridHeader(x => x.IsEnable),
                this.MakeGridHeader(x => x.DllZipName),
                this.MakeGridHeader(x => x.DllHash),
                this.MakeGridHeader(x => x.IsSwallow),
                this.MakeGridHeaderAction(width: 200)
            };
        }

        public override IOrderedQueryable<SysDeviceView> GetSearchQuery()
        {
            var query = DC.Set<SysDevice>()
                .CheckContain(Searcher.Name, x=>x.Name)
                .CheckContain(Searcher.Model, x=>x.Model)
                .CheckEqual(Searcher.Mode, x=>x.Mode)
                .CheckEqual(Searcher.GroupId, x=>x.GroupId)
                .CheckEqual(Searcher.Port, x=>x.Port)
                .CheckContain(Searcher.DllName, x=>x.DllName)
                .CheckContain(Searcher.DllClass, x=>x.DllClass)
                .CheckContain(Searcher.DllWorkSpace, x=>x.DllWorkSpace)
                .CheckEqual(Searcher.IsEnable, x=>x.IsEnable)
                .Select(x => new SysDeviceView
                {
				    ID = x.ID,
                    Name = x.Name,
                    Model = x.Model,
                    Description = x.Description,
                    Mode = x.Mode,
                    GroupName = x.Group.Name,
                    Port = x.Port,
                    DllName = x.DllName,
                    DllZipId = x.DllZipId,
                    DllClass = x.DllClass,
                    DllWorkSpace = x.DllWorkSpace,
                    DllHash = x.DllHash,
                    DllZipName = x.DllZip.FileName,
                    IsEnable = x.IsEnable,
                    IsSwallow = x.IsSwallow
                })
                .OrderBy(x => x.ID);
            return query;
        }

    }

    public class SysDeviceView : SysDevice{
        [Display(Name = "分类名称")]
        public String GroupName { get; set; }

        [Display(Name = "压缩文件名称")]
        public string DllZipName { get; set; }
    }
}
